import { CommonModal } from "@/pages/OrderModule/common/components/CommonModal";
import SubTitle from "@/pages/OrderModule/common/components/SubTitle";
import { Col, Form, Radio, Row, Modal, message, Typography } from "antd";
import OrderAreaRadioGroup from "../../components/OrderAreaRadioGroup1";
import OrderNumberCount from "../../components/OrderNumberCount";
import { useState, useEffect, useMemo, useRef } from "react";
import { orderReanalysis } from "../../../../services";
import CommonLoading from "@/components/CommonLoading";
import { find } from "lodash";
import CommonTable from "@/components/CommonTable";
import ResultModal from "./ResultModal";

const { Paragraph } = Typography;

/** 订单重算 */
function WeightRecalculation(props) {
	const [form] = Form.useForm();

	const [loading, setLoading] = useState(false);
	const [showNumber, setShowNumber] = useState({});
	const filterIds = useRef(undefined);
	const [failList, setFailList] = useState([]);
	const [visible, setVisible] = useState(false);

	const { orderList, searchForm, selectKeys } = props;

	function handleOk() {
		handleSave();
	}

	const handleSave = async () => {
		let params = { orderIds: filterIds.current };
		setLoading(true);
		const { msg, success, data = {} } = await orderReanalysis(params);
		setLoading(false);
		if (success) {
			const { recountItems = [] } = data;
			if (recountItems.length) {
				setVisible(true);
				setFailList(recountItems);
			} else {
				message.success(msg);
				props.handleOk(1, filterIds.current);
			}
		}
	};

	const orderAreaRadioProps = {
		searchForm,
		selectKeys,
		orderList,
		states: [10, 30],
		title: "仅限待审核、异常状态订单",
		updateShowNumer(obj, ids) {
			filterIds.current = ids;
			setShowNumber(obj);
		}
	};

	//处理结果弹窗
	const ResultModalProps = {
		failList,
		handleCancel() {
			setVisible(false);
			props.handleOk(1, filterIds.current);
		}
	};

	return (
		<CommonModal title={{ title: "订单重算" }} open={true} width="530px" onOk={handleOk} onCancel={props.handleCancel}>
			<div>
				<OrderAreaRadioGroup {...orderAreaRadioProps} />
				<div style={{ marginTop: "16px" }}>
					<OrderNumberCount
						data={[
							{ label: "待审核", value: showNumber[10], type: "warning" },
							{ label: "异常", value: showNumber[30], type: "error" }
						]}
					/>
				</div>
				<SubTitle symbol as="s2" title="订单重算" wrapStyle={{ marginTop: "16px", marginBottom: "16px" }} />
				<p>
					重算时，会按照当前订单已有的原始商品（拆合单不会回退），重新查找{" "}
					<span className="common-color9">系统商品或组合商品的绑定关系</span>
					，再重新读取 <span className="common-color9">匹配关系</span>，最后重新计算
					<span className="common-color9">自动策略</span>。
				</p>
				<p style={{ marginTop: 16 }}>注：订单如果<span className="common-color9">“已回传”</span>，则发货仓、快递和运单号都不会变</p>
			</div>
			<CommonLoading loading={loading} />
			{visible && <ResultModal {...ResultModalProps} />}
		</CommonModal>
	);
}

export default WeightRecalculation;
